package com.hxexp.weixin.mvc.bindpassenger.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.constant.Privilege;
import com.hxexp.weixin.constant.WebAppVersion;
import com.hxexp.weixin.mvc.bindpassenger.model.BindPassengerAjaxResult;
import com.hxexp.weixin.mvc.bindpassenger.service.BindPassengerService;
import com.hxexp.weixin.mvc.passenger.model.OrderPaymentInfoBean;
import com.hxexp.weixin.mvc.passenger.model.PassengerInfoBean;
import com.hxexp.weixin.mvc.passenger.model.PassengerInfoParam;

/**
 * 绑定乘客控制器
 *
 * @author wang.bin
 *
 */
@Controller
@RequestMapping(value = "/bindpassenger")
public final class BindPassengerController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BindPassengerController.class);

    @Autowired
    private BindPassengerService bindPassengerService;

    @ModelAttribute("constant")
    public Map<String, String> setConstant() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appver_nc", WebAppVersion.NC);// 南充当代运业
        map.put("appver_ylns", WebAppVersion.YLNS); // 四川仪陇农商银行

        return map;
    }
    /**
     * 绑定乘客
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/bind")
    @ResponseBody
    public BindPassengerAjaxResult bindPassenger(String orderId, String passengerIds, String userId, Model model) {

        BindPassengerAjaxResult result = new BindPassengerAjaxResult();
        try {
            result = bindPassengerService.bindPassenger(orderId, passengerIds, userId);
        } catch (ServiceException e) {
            LOGGER.error("调用绑定乘车人是发生异常", e);
        }
        return result;
    }

    /**
     * 绑定乘客确认页面
     *
     * @param model
     * @return 绑定乘客确认页面视图名
     */
    @RequestMapping(value = "/index")
    public String index(PassengerInfoParam passangers, OrderPaymentInfoBean orderPaymentInfo, Model model) {
        List<PassengerInfoBean> passengerList = passangers.getPassengers();
        List<PassengerInfoBean> selectPassengerList = new ArrayList<PassengerInfoBean>();

        for (PassengerInfoBean passenger : passengerList) {
            if (passenger.getCheckbox() != null) {
                if (StringUtils.isEmpty(passenger.getPassengerId())) {
                    passenger.setPassengerId(passenger.getUserId());
                }
                selectPassengerList.add(passenger);
            }
        }
        if (orderPaymentInfo.isHasPrivilege()) {
            model.addAttribute("hasPrivilege", true);
            model.addAttribute("privilege_amount", Privilege.ULLINE_PRIVILEGE_AMOUNT);// 优惠金额
        }
        model.addAttribute("passengerList", selectPassengerList);
        model.addAttribute("orderPaymentInfo", orderPaymentInfo);
        model.addAttribute("userId", selectPassengerList.get(0).getUserId());
        return "bindpassenger";
    }
}
